﻿
Partial Class WebForm_WorkHeader
    Inherits System.Web.UI.Page

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        If clsAuth.GetUserID() <> "" Then
            Me.MasterPageFile = "~\WebForm\MasterPage.master"
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            '判斷是否有找到
            If DirectCast(Master.FindControl("lblHeadText"), Label) IsNot Nothing Then
                DirectCast(Master.FindControl("lblHeadText"), Label).Text = Me.GetLocalResourceObject("Title.Text")
            End If
            Me.InitData()
        End If
    End Sub

    Protected Sub SearchWorkHeader_RowUpdating(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchWorkHeader1.RowUpdating
        '單筆檢視模式
        Me.TabContainer1.ActiveTab = Me.AddTabPanel
        Me.MultiView1.SetActiveView(vwHandleWorkHeader)
        Me.HandleWorkHeader1.DataAccessMode = My.DataAccessMode.Update
        Me.HandleWorkHeader1.InitData(Me.SearchWorkHeader1.FindDirtyHeaderID, "")
    End Sub

    Protected Sub HandleWorkHeader1_Back(ByVal sender As Object, ByVal e As System.EventArgs) Handles HandleWorkHeader1.Back
        '列表檢視模式
        Me.TabContainer1.ActiveTab = Me.SearchTabPanel
    End Sub

    Protected Sub AddWorkHeader1_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddWorkHeader1.Saving
        If Me.SaveData = True Then
            '列表檢視模式
            'Me.TabContainer1.ActiveTab = Me.SearchTabPanel
            Me.SearchWorkHeader1.BindGV(Me.AddWorkHeader1.RandomCasePassword)
            Me.MultiView1.SetActiveView(vwHandleWorkHeader)
            Me.HandleWorkHeader1.DataAccessMode = My.DataAccessMode.Update
            Me.HandleWorkHeader1.InitData(Me.SearchWorkHeader1.FindDirtyHeaderID, "")

            MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_Success"))
        End If
    End Sub

    Protected Sub lknAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lknAdd.Click
        Me.TabContainer1.ActiveTab = Me.AddTabPanel
        Me.MultiView1.SetActiveView(vwAddWorkHeader)
        Me.InitData()
    End Sub
End Class

Partial Class WebForm_WorkHeader
    Public Sub InitData()
        Me.AddWorkHeader1.DataAccessMode = My.DataAccessMode.Insert
        Me.AddWorkHeader1.InitData()
    End Sub

    Protected Sub SearchWorkHeader_RowDeleting(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchWorkHeader1.RowDeleting
        Dim dat As New clsData
        Dim sql As New StringBuilder
        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("Delete FD_FindDirtyHeader ")
            sql.AppendFormat(" where FindDirtyHeaderID='{0}' ", Me.SearchWorkHeader1.FindDirtyHeaderID)
            dat.AccData(sql)
            sql.Length = 0
            sql.AppendFormat("Delete FD_FindDirtyItem ")
            sql.AppendFormat(" where FindDirtyHeaderID='{0}' ", Me.SearchWorkHeader1.FindDirtyHeaderID)
            dat.AccData(sql)
            scope.Complete()
        End Using
        Me.SearchWorkHeader1.BindGV()
    End Sub

    Private Function SaveData() As Boolean
        Dim dat As New clsData
        Dim sql As New StringBuilder

        Using scope As New Transactions.TransactionScope
            If (Me.AddWorkHeader1.DataAccessMode = My.DataAccessMode.Insert) Then
                sql.Length = 0
                sql.AppendFormat("insert FD_FindDirtyHeader (RandomCasePassword,CasePassword,RegionType,TownID,VillageID,RoadItemID,Location,Status,ReportDate,Topic,Description,CreateDate,CreateBy,CreateTEL,CreateEmail,Active) select ")
                sql.AppendFormat(" '{0}' ", Me.AddWorkHeader1.RandomCasePassword)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.CasePassword)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.RegionType)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.TownID)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.RoadID)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.RoadItemID)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.Location)
                sql.AppendFormat(",'{0}' ", "P")
                'sql.AppendFormat(",'{0:yyyy/MM/dd}' ", Me.AddWorkHeader1.ReportDate)
                sql.AppendFormat(",{0} ", "getdate()")
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.Topic)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.Description)
                sql.AppendFormat(",{0} ", "getdate()")
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.CreateBy)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.CreateTEL)
                sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.CreateEmail)
                sql.AppendFormat(",'{0}' ", "Y")
                dat.AccData(sql)
                Dim id As Integer = MyObj.GetIdentity()

                '寫入資料到FD_FindDirtyItem
                For i As Integer = 0 To Me.AddWorkHeader1.DirtyItem.Length / 2 - 1
                    If Me.AddWorkHeader1.DirtyItem(i, 0) <> "" Then
                        sql.Length = 0
                        sql.AppendFormat("insert FD_FindDirtyItem (FindDirtyHeaderID,DirtyItemName,DirtyLevel) select ")
                        sql.AppendFormat(" '{0}' ", id)
                        sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 0))
                        sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 1))
                        dat.AccData(sql)
                    End If
                Next

                '寫入資料到FD_ActionRecord,若為道路,則須另寫入該路段需處理的單位
                sql.Length = 0
                sql.AppendFormat("insert into FD_ActionRecord(CasePassword,OrganizationID,Status,Active,CreateBy,CreateDate) ")
                sql.AppendFormat("select a.CasePassword,b.OrganizationID,'P' as Status,'Y' as Active,'{0}',Getdate() from FD_FindDirtyHeader a ", Me.LoginID)
                sql.AppendFormat("join FD_OrganizationRegionMapping b ")
                sql.AppendFormat("on a.TownID=b.RegionID ")
                sql.AppendFormat("where a.FindDirtyHeaderID='{0}' ", id)
                sql.AppendFormat(" union ")
                sql.AppendFormat("select a.CasePassword,b.OrganizationID,'P' as Status,'Y' as Active,'{0}',Getdate() from FD_FindDirtyHeader a ", Me.LoginID)
                sql.AppendFormat("join FD_OrganizationRoadMapping b ")
                sql.AppendFormat("on a.VillageID=b.RoadID ")
                sql.AppendFormat("where a.RegionType='道路' ")
                sql.AppendFormat("and a.FindDirtyHeaderID='{0}' ", id)
                dat.AccData(sql)

            ElseIf (Me.AddWorkHeader1.DataAccessMode = My.DataAccessMode.Update) Then
                sql.Length = 0
                sql.AppendFormat("Update FD_FindDirtyHeader set")
                sql.AppendFormat(" RegionType='{0}' ", Me.AddWorkHeader1.RegionType)
                sql.AppendFormat(" ,TownID='{0}' ", Me.AddWorkHeader1.TownID)
                sql.AppendFormat(" ,VillageID='{0}' ", Me.AddWorkHeader1.RoadID)
                sql.AppendFormat(" ,RoadItemID='{0}' ", Me.AddWorkHeader1.RoadItemID)
                sql.AppendFormat(" ,Location='{0}' ", Me.AddWorkHeader1.Location)
                'sql.AppendFormat(" ,ReportDate='{0:yyyy/MM/dd}' ", Me.AddWorkHeader1.ReportDate)
                sql.AppendFormat(" ,Topic='{0}' ", Me.AddWorkHeader1.Topic)
                sql.AppendFormat(" ,Description='{0}' ", Me.AddWorkHeader1.Description)
                sql.AppendFormat(" ,CreateBy='{0}' ", Me.AddWorkHeader1.CreateBy)
                sql.AppendFormat(" ,CreateTEL='{0}' ", Me.AddWorkHeader1.CreateTEL)
                sql.AppendFormat(" ,CreateEmail='{0}' ", Me.AddWorkHeader1.CreateEmail)
                sql.AppendFormat(" ,UpdateDate={0} ", "getdate()")
                sql.AppendFormat(" ,UpdateBy='{0}' ", Me.LoginID)
                sql.AppendFormat(" where FindDirtyHeaderID='{0}' ", Me.SearchWorkHeader1.FindDirtyHeaderID)
                dat.AccData(sql)

                sql.Length = 0
                sql.AppendFormat("Delete FD_FindDirtyItem ")
                sql.AppendFormat(" where FindDirtyHeaderID='{0}' ", Me.SearchWorkHeader1.FindDirtyHeaderID)
                dat.AccData(sql)
                For i As Integer = 0 To Me.AddWorkHeader1.DirtyItem.Length / 2 - 1
                    If Me.AddWorkHeader1.DirtyItem(i, 0) <> "" Then
                        sql.Length = 0
                        sql.AppendFormat("insert FD_FindDirtyItem (FindDirtyHeaderID,DirtyItemName,DirtyLevel) select ")
                        sql.AppendFormat(" '{0}' ", Me.SearchWorkHeader1.FindDirtyHeaderID)
                        sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 0))
                        sql.AppendFormat(",'{0}' ", Me.AddWorkHeader1.DirtyItem(i, 1))
                        dat.AccData(sql)
                    End If
                Next
            End If
            scope.Complete()
        End Using

        Return True
    End Function

    Private ReadOnly Property LoginID() As String
        Get
            Return Context.User.Identity.Name
        End Get
    End Property

End Class